// 删除指定的元素
export const remove = (arr: any, ele: any) => {
  const index = arr.indexOf(ele);
  if (index > -1) {
    arr.splice(index, 1);
  }
  return arr;
};

// 求数组的最大值
export const max = arr => {
  return Math.max.apply(null, arr);
};

// 求数组的最小值
export const min = arr => {
  return Math.min.apply(null, arr);
};

// 数组求和
export const sum = arr => {
  return arr.reduce((pre, cur) => {
    return pre + cur;
  });
};

// 数组求平均值
export const average = arr => {
  return sum(arr) / arr.length;
};

// 数组去重
export const unique = (arr: any) => {
  if (Array.hasOwnProperty("from")) {
    return Array.from(new Set(arr));
  } else {
    // tslint:disable-next-line: one-variable-per-declaration
    let n: any = {},
      r: any = [];
    for (let i = 0; i < arr.length; i++) {
      if (!n[arr[i]]) {
        n[arr[i]] = true;
        r.push(arr[i]);
      }
    }
    return r;
  }
};

// 求两个集合的并集
export const union = (a, b) => {
  let newArr = a.concat(b);
  return unique(newArr);
};

// 数组排序 43.数组排序，{type} 1：从小到大 2：从大到小 3：随机
export const sort = (arr, type = 1) => {
  return arr.sort((a, b) => {
    switch (type) {
      case 1:
        return a - b;
      case 2:
        return b - a;
      case 3:
        return Math.random() - 0.5;
      default:
        return arr;
    }
  });
};

// 判断一个数是否在数组当中
export const contains = (arr, val) => {
  return arr.indexOf(val) !== -1 ? true : false;
};
